﻿#include "MirAmountBox.h"
#include "../MirGraphics/MLibrary.h"
#include "../Settings.h"
#include "../MirSounds/SoundManager.h"
#include "MirScene.h"
#include "../Program.h"

using namespace Client::MirGraphics;
using namespace Client::MirSounds;

namespace Client::MirControls
{

    MirAmountBox::MirAmountBox(const std::string &title, int image, unsigned int max, unsigned int min, unsigned int defaultAmount)
    {
        ImageIndex = image;
        MaxAmount = max;
        MinAmount = min;
        Amount = max;
        setModal(true);
        setMovable(false);

        setIndex(238);
        setLibrary(Libraries::Prguse);

        Point tempVar((Settings::ScreenWidth - getSize()->Width) / 2, (Settings::ScreenHeight - getSize()->Height) / 2);
        setLocation(&tempVar);

        TitleLabel = new MirLabel();
        TitleLabel->setAutoSize(true);
        Point tempVar2(19, 8);
        TitleLabel->setLocation(&tempVar2);
        TitleLabel->setParent(this);
        TitleLabel->setNotControl(true);
        TitleLabel->setText(title);

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar3(180, 3);
        CloseButton->setLocation(&tempVar3);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Dispose();
        };

        ItemImage = new MirControl();
        Point tempVar4(15, 34);
        ItemImage->setLocation(&tempVar4);
        Size tempVar5(38, 34);
        ItemImage->setSize(&tempVar5);
        ItemImage->setParent(this);
        ItemImage->AfterDraw += [&] (o, e)
        {
            DrawItem();
        };

        OKButton = new MirButton();
        OKButton->setHoverIndex(201);
        OKButton->setIndex(200);
        OKButton->setLibrary(Libraries::Title);
        Point tempVar6(23, 76);
        OKButton->setLocation(&tempVar6);
        OKButton->setParent(this);
        OKButton->setPressedIndex(202);
        OKButton->Click += [&] (o, e)
        {
            Dispose();
        };

        CancelButton = new MirButton();
        CancelButton->setHoverIndex(204);
        CancelButton->setIndex(203);
        CancelButton->setLibrary(Libraries::Title);
        Point tempVar7(110, 76);
        CancelButton->setLocation(&tempVar7);
        CancelButton->setParent(this);
        CancelButton->setPressedIndex(205);
        CancelButton->Click += [&] (o, e)
        {
            Dispose();
        };

        InputTextBox = new MirTextBox();
        InputTextBox->setParent(this);
        InputTextBox->setBorder(true);
        InputTextBox->setBorderColour(Color::Lime);
        Point tempVar8(58, 43);
        InputTextBox->setLocation(&tempVar8);
        Size tempVar9(132, 19);
        InputTextBox->setSize(&tempVar9);
        InputTextBox->SetFocus();
        InputTextBox->TextBox->KeyPress += MirInputBox_KeyPress;
        InputTextBox->TextBox->TextChanged += TextBox_TextChanged;
        InputTextBox->setText((defaultAmount > 0 && defaultAmount <= MaxAmount) ? std::to_string(defaultAmount) : std::to_string(MaxAmount));
        InputTextBox->TextBox->SelectionStart = 0;
        InputTextBox->TextBox->SelectionLength = InputTextBox->getText().length();

    }

    MirAmountBox::MirAmountBox(const std::string &title, int image, const std::string &message)
    {
        ImageIndex = image;

        setModal(true);
        setMovable(false);

        setIndex(238);
        setLibrary(Libraries::Prguse);

        Point tempVar((800 - getSize()->Width) / 2, (600 - getSize()->Height) / 2);
        setLocation(&tempVar);



        TitleLabel = new MirLabel();
        TitleLabel->setAutoSize(true);
        Point tempVar2(19, 8);
        TitleLabel->setLocation(&tempVar2);
        TitleLabel->setParent(this);
        TitleLabel->setNotControl(true);
        TitleLabel->setText(title);

        TextLabel = new MirLabel();
        TextLabel->setAutoSize(true);
        Point tempVar3(60, 43);
        TextLabel->setLocation(&tempVar3);
        TextLabel->setForeColour(Color::Yellow);
        TextLabel->setParent(this);
        TextLabel->setNotControl(true);
        TextLabel->setText(message);

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar4(180, 3);
        CloseButton->setLocation(&tempVar4);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Dispose();
        };

        ItemImage = new MirControl();
        Point tempVar5(15, 34);
        ItemImage->setLocation(&tempVar5);
        Size tempVar6(38, 34);
        ItemImage->setSize(&tempVar6);
        ItemImage->setParent(this);
        ItemImage->AfterDraw += [&] (o, e)
        {
            DrawItem();
        };

        OKButton = new MirButton();
        OKButton->setHoverIndex(201);
        OKButton->setIndex(200);
        OKButton->setLibrary(Libraries::Title);
        Point tempVar7(23, 76);
        OKButton->setLocation(&tempVar7);
        OKButton->setParent(this);
        OKButton->setPressedIndex(202);
        OKButton->Click += [&] (o, e)
        {
            Dispose();
        };

        CancelButton = new MirButton();
        CancelButton->setHoverIndex(204);
        CancelButton->setIndex(203);
        CancelButton->setLibrary(Libraries::Title);
        Point tempVar8(110, 76);
        CancelButton->setLocation(&tempVar8);
        CancelButton->setParent(this);
        CancelButton->setPressedIndex(205);
        CancelButton->Click += [&] (o, e)
        {
            Dispose();
        };
    }

    void MirAmountBox::TextBox_TextChanged(std::any sender, EventArgs *e)
    {
        if (unsigned int::TryParse(InputTextBox->getText(), Amount) && Amount >= MinAmount)
        {
            InputTextBox->setBorderColour(Color::Lime);

            OKButton->setVisible(true);
            if (Amount > MaxAmount)
            {
                Amount = MaxAmount;
                InputTextBox->setText(std::to_string(MaxAmount));
                InputTextBox->TextBox->SelectionStart = InputTextBox->getText().length();
            }

            if (Amount == MaxAmount)
            {
                InputTextBox->setBorderColour(Color::Orange);
            }
        }
        else
        {
            InputTextBox->setBorderColour(Color::Red);
            OKButton->setVisible(false);
        }
    }

    void MirAmountBox::MirInputBox_KeyPress(std::any sender, KeyPressEventArgs *e)
    {
        if (!std::iscntrl(e->KeyChar) && !std::isdigit(e->KeyChar))
        {
            e->Handled = true;
        }

        if (e->KeyChar == static_cast<char>(Keys->Enter))
        {
            if (OKButton != nullptr && !OKButton->isDisposed())
            {
                OKButton->InvokeMouseClick(EventArgs::Empty);
            }
            e->Handled = true;
        }
        else if (e->KeyChar == static_cast<char>(Keys->Escape))
        {
            if (CancelButton != nullptr && !CancelButton->isDisposed())
            {
                CancelButton->InvokeMouseClick(EventArgs::Empty);
            }
            e->Handled = true;
        }
    }

    void MirAmountBox::DrawItem()
    {
        int x = ItemImage->getDisplayLocation()->X, y = ItemImage->getDisplayLocation()->Y;

        Size *s = Libraries::Items->GetTrueSize(ImageIndex);

        x += (ItemImage->getSize()->Width - s->Width) / 2;
        y += (ItemImage->getSize()->Height - s->Height) / 2;

        Libraries::Items->Draw(ImageIndex, x, y);
    }

    void MirAmountBox::Show()
    {
        if (getParent() != nullptr)
        {
            return;
        }

        setParent(MirScene::ActiveScene);

        Highlight();

        for (int i = 0; i < Program::Form->Controls->Count; i++)
        {
            TextBox *T = dynamic_cast<TextBox*>(Program::Form->Controls[i]);
            if (T != nullptr && T->Tag != nullptr && T->Tag != nullptr)
            {
                (static_cast<MirTextBox*>(T->Tag))->DialogChanged();
            }
        }

        /*
        CMain.Shift = false;
        CMain.Ctrl = false;
        CMain.Alt = false;
    
        Parent = MirScene.ActiveScene;
        Activate();
        Highlight();
    
        for (int i = 0; i < Main.This.Controls.Count; i++)
        {
            TextBox T = (TextBox)Main.This.Controls[i];
            if (T != null && T.Tag != null && T.Tag != null)
                ((MirTextBox)T.Tag).DialogChanged();
        }*/
    }

    void MirAmountBox::OnKeyDown(KeyEventArgs *e)
    {
        MirImageControl::OnKeyDown(e);
        e->Handled = true;
    }

    void MirAmountBox::OnKeyUp(KeyEventArgs *e)
    {
        MirImageControl::OnKeyUp(e);
        e->Handled = true;
    }

    void MirAmountBox::OnKeyPress(KeyPressEventArgs *e)
    {
        MirImageControl::OnKeyPress(e);

        if (e->KeyChar == static_cast<char>(Keys->Escape))
        {
            CancelButton->InvokeMouseClick(EventArgs::Empty);
        }
        else if (e->KeyChar == static_cast<char>(Keys->Enter))
        {
            OKButton->InvokeMouseClick(EventArgs::Empty);
        }
        e->Handled = true;
    }

    void MirAmountBox::Dispose(bool disposing)
    {
        MirImageControl::Dispose(disposing);

        if (!disposing)
        {
            return;
        }

        for (int i = 0; i < Program::Form->Controls->Count; i++)
        {
            TextBox *T = static_cast<TextBox*>(Program::Form->Controls[i]);
            if (T != nullptr && T->Tag != nullptr && T->Tag != nullptr)
            {
                (static_cast<MirTextBox*>(T->Tag))->DialogChanged();
            }
        }
    }
}
